package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;

import users.Clerk;

public class returnTabGui extends JPanel{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Clerk clerk;
	
	private JPanel jp1;
	
	private JButton returnButton;
	
	private JTextField receiptID, upc, quantity, storeName;
	
	private JTextArea textArea;
	
	private JScrollPane scrollPane;
	
	public returnTabGui(Clerk clerk)
	{
		this.clerk  = clerk;
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		initJp1();
		initJp2();
	}
	
	// where one text field for receipt ID, and button that will output the result of return 
	public void initJp1()
	{
		jp1 = new JPanel();
		jp1.add(new JLabel("Receipt ID :"));
		jp1.setLayout(new GridLayout (5,2));
		jp1.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		
		receiptID = new JTextField();
        receiptID.setPreferredSize(new Dimension(300,30));
        jp1.add(receiptID);
        
        jp1.add(new JLabel("upc: "));
        
        upc = new JTextField();
        upc.setPreferredSize(new Dimension(300,30));
        jp1.add(upc);
        
        jp1.add(new JLabel("quantity: "));
        quantity = new JTextField();
        quantity.setPreferredSize(new Dimension(300,30));
        jp1.add(quantity);
        
        jp1.add(new JLabel("Store name: "));
        storeName = new JTextField();
        storeName.setPreferredSize(new Dimension(300,30));
        jp1.add(storeName);
        
        returnButton = new JButton("Return");
        returnButton.addActionListener(new ProcessReturn());
        jp1.add(returnButton);
        add(jp1);
        
                       
	}
	public void initJp2()
	{
		JPanel jp2 = new JPanel();
		textArea = new JTextArea(20, 1);
		scrollPane = new JScrollPane(textArea);
		
		
		jp2.setLayout(new BorderLayout());
		jp2.add(scrollPane, BorderLayout.CENTER);
		
		
		add(jp2);
	}

	private class ProcessReturn implements ActionListener{
	
		public void actionPerformed(ActionEvent e) {
			
			// splitting text fields into an array so that it can be differentiated. 
		
			String receipt = receiptID.getText();
			String upc1 = upc.getText();
			String storeName1 = storeName.getText();
			int quantity1 = Integer.parseInt(quantity.getText());
			int error = -1;
			
			try {
				error = clerk.checkReturn(receipt, upc1, quantity1, storeName1);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			if(error == 0){
				JOptionPane.showMessageDialog(returnButton,
					    "Return is ok",
					    "Return successful",
					    JOptionPane.OK_OPTION);
						return;
			} else if (error == 1){
				JOptionPane.showMessageDialog(returnButton,
					    "The item does not exist in this purchase",
					    "Return unsuccessful",
					    JOptionPane.ERROR_MESSAGE);
						return;
			} else if (error == 2){
				JOptionPane.showMessageDialog(returnButton,
					    "Return date passed.",
					    "Return successful",
					    JOptionPane.ERROR_MESSAGE);
						return;
			}
		}
	}
}
